package com.tac.service.scm.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tac.common.result.Result;
import com.tac.dao.scm.SupplierDao;
import com.tac.pojo.scm.Supplier;
import com.tac.service.scm.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {


    @Autowired
    private SupplierDao supplierDao;

    //查看供应商列表
    public Result<PageInfo<Supplier>> getList(Integer pageNum, Integer pageSize, String supplierName, String contactPerson){
        PageHelper.startPage(pageNum,pageSize);
        List<Supplier> list = supplierDao.getList(supplierName, contactPerson);
        PageInfo<Supplier> supplierPageInfo = new PageInfo<>(list);
        return Result.success(supplierPageInfo);
    }
    //修改供应商信息
    public Result update(Supplier supplier){
        if(supplier.getApplicationStatus().equals("已审核")){
            return Result.error("权限不足");
        }
        if(supplier.getApplicationStatus().equals("待审核")){
            //用户点了提交
            //设置申请时间
            supplier.setApplicationDate(new Date());
        }
        Integer integer = supplierDao.update(supplier);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("修改供应商信息失败");
    }
    //删除供应商
    public Result remove(List<Integer> ids){
        Integer integer = supplierDao.remove(ids);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("删除供应商失败");
    }
    //添加供应商
    public Result save(Supplier supplier){
        //申请人先填假数据
        supplier.setApplication("童岸尘");
        if(supplier.getApplicationStatus().equals("待审核")){
            //用户新建后点了提交
            //设置申请时间
            supplier.setApplicationDate(new Date());
        }
        if(supplier.getApplicationStatus().equals("待提交")){
            //待提交不能设置申请时间
            supplier.setApplicationDate(null);
        }
        if(supplier.getApplicationStatus().equals("已审核")){
            return Result.error("权限不足");
        }
        Integer integer = supplierDao.save(supplier);
        if(integer>0){
            return Result.success(supplier);
        }
        return Result.error("添加供应商失败");
    }

    //管理员审核
    public Result updateExamine(Supplier supplier){
        //首先是供应商是一个待审核的状态，并且修改成已审核状态
        Supplier byId = supplierDao.getById(supplier.getId());
        if(byId.getApplicationStatus().equals("未提交")){
            return Result.error("未提交不能审核");
        }else if(byId.getApplicationStatus().equals("待审核")){
            supplier.setApplicationStatus("已审核");
            //然后添加假审核人审核时间
            supplier.setExamine("管理员");
        }
        supplier.setExamineDate(new Date());
        Integer integer = supplierDao.updateExamine(supplier);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("管理员审核失败");
    }
}
